import { Message } from 'element-ui'

export const downloadFile = async(response) => {
  const filename = new Date().getTime() + '.xlsx'
  if (response.data.size == 0) {
    Message.error('下载失败: 服务器出错!')
    return reject()
  }
  if ('msSaveOrOpenBlob' in navigator) {
    // 文件名字为时间.xlsx
    window.navigator.msSaveOrOpenBlob(response.data, filename)
    return
  }
  const blobUrl = window.URL.createObjectURL(new Blob([response.data]))
  const link = document.createElement('a')
  link.style.display = 'none'
  link.href = blobUrl
  link.setAttribute('download', filename)
  document.body.appendChild(link)
  link.click()
  link.remove()
  window.URL.revokeObjectURL(blobUrl)
}
